﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCinema.Data;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace TinyCinema.Bussiness.Promotion
{
    public class Promotions :DependencyObject
    {
        public static DependencyProperty lstPromotionProperty = DependencyProperty.Register("lstPromotion", typeof(ObservableCollection<Data.Promotion>), typeof(Promotions));
        public ObservableCollection<Data.Promotion> lstPromotion
        {
            get { return (ObservableCollection<Data.Promotion>)GetValue(lstPromotionProperty); }
            set { SetValue(lstPromotionProperty, value); }
        }
        public static DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(bool), typeof(Promotions));
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set
            {
                SetValue(IsEditableProperty, value);
            }
        }
        public static DependencyProperty CurrentPromotionProperty = DependencyProperty.Register("CurrentPromotion", typeof(Data.Promotion), typeof(Promotions));
        public Data.Promotion CurrentPromotion
        {
            get { return (Data.Promotion)GetValue(CurrentPromotionProperty); }
            set
            {
                SetValue(CurrentPromotionProperty, value);
            }
        }
        public static DependencyProperty CurrentViewProperty = DependencyProperty.Register("CurrentView", typeof(Data.Promotion), typeof(Promotions));
        public Data.Promotion CurrentView
        {
            get { return (Data.Promotion)GetValue(CurrentViewProperty); }
            set
            {
                SetValue(CurrentViewProperty, value);
                if (value == null) return;
                if ((value as Data.Promotion).ID == null) IsEditable = true;
                else IsEditable = false;
            }
        }
        public static DependencyProperty IsViewModeEnableProperty = DependencyProperty.Register("IsViewModeEnable", typeof(bool), typeof(Promotions));
        public bool IsViewModeEnable
        {
            get { return (bool)GetValue(IsViewModeEnableProperty); }
            set
            {
                SetValue(IsViewModeEnableProperty, value);
            }
        }
        public static DependencyProperty strSearchProperty = DependencyProperty.Register("strSearch", typeof(string), typeof(Promotions));
        public string strSearch
        {
            get { return (string)GetValue(strSearchProperty); }
            set
            {
                SetValue(strSearchProperty, value);
            }
        }
        public static DependencyProperty IsSearchingProperty = DependencyProperty.Register("IsSearching", typeof(bool), typeof(Promotions));
        public bool IsSearching
        {
            get { return (bool)GetValue(IsSearchingProperty); }
            set
            {
                SetValue(IsSearchingProperty, value);
            }
        }
        public static DependencyProperty SearchFilterProperty = DependencyProperty.Register("SearchFilter", typeof(ObservableCollection<String>), typeof(Promotions));
        public ObservableCollection<String> SearchFilter
        {
            get { return (ObservableCollection<String>)GetValue(SearchFilterProperty); }
            set { SetValue(SearchFilterProperty, value); }
        }
        public static DependencyProperty SearchFilterIndexProperty = DependencyProperty.Register("SearchFilterIndex", typeof(int), typeof(Promotions));
        public int SearchFilterIndex
        {
            get { return (int)GetValue(SearchFilterIndexProperty); }
            set { SetValue(SearchFilterIndexProperty, value); }
        }
        public static DependencyProperty CurrentViewIndexProperty = DependencyProperty.Register("CurrentViewIndex", typeof(int), typeof(Promotions));
        public int CurrentViewIndex
        {
            get { return (int)GetValue(CurrentViewIndexProperty); }
            set
            {
                if (value < 1 || value > lstPromotion.Count) return;
                try { CurrentView = lstPromotion[value - 1]; }
                catch { return; }
                SetValue(CurrentViewIndexProperty, value);
            }
        }
        public ICommand ChoosePromotionCommand { get; set; }
        public ICommand CreateCommand { get; set; }
        public ICommand EditCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ICommand BackCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand AddPhotoCommand { get; set; }
        public ICommand PreviousCommand { get; set; }
        public ICommand NextCommand { get; set; }
        public ICommand GoToPageCommand { get; set; }
        public ICommand SearchCommand { get; set; }
        public ICommand SearchCancelCommand { get; set; }
        bool IsCreating;
        public Messenger Messenger { get; set; }
        public Promotions()
        {
            Messenger = new Bussiness.Messenger();
            lstPromotion = Data.Promotion.GetAllPromotion();
            foreach (var promotion in lstPromotion)
            {
                if (promotion.StartDate <= DateTime.Now && promotion.EndDate >=DateTime.Now)
                {
                    CurrentPromotion = promotion;
                    break;
                }
            }
            IsEditable = true; // Disable nút Edit
            IsViewModeEnable = true;
            ChoosePromotionCommand = new RelayCommand<Data.Promotion>(
                    (s) => true,
                    (s) =>
                    {
                        CurrentViewIndex = lstPromotion.IndexOf(s) + 1;
                        CurrentView = s;
                        IsEditable = false;
                        IsViewModeEnable = false;
                    }
                );
                CreateCommand = new RelayCommand<Data.Promotion>(
                    (s) => IsViewModeEnable,
                    (s) =>
                    {
                        CurrentView = new Data.Promotion();
                        IsEditable = true;
                        IsViewModeEnable = false;
                        IsCreating = true;
                    }
                );
                EditCommand = new RelayCommand<Data.Promotion>(
                    (s) => !IsEditable&&!IsViewModeEnable,
                    (s) =>
                    {
                        IsEditable = true;
                    }
                );
                SaveCommand = new RelayCommand<Data.Promotion>(
                    (s) => true,
                    (s) =>
                {
                    if (lstPromotion.IndexOf(CurrentView) ==-1 ) lstPromotion.Add(CurrentView);
                    if (IsCreating)
                    {
                        CurrentViewIndex = lstPromotion.IndexOf(CurrentView) + 1;
                        IsCreating = false;
                    }
                    CurrentView.Save();
                    IsEditable = false;
                }
                );
                BackCommand = new RelayCommand<Data.Promotion>(
                (s) => true,
                (s) =>
                {
                    if (IsEditable)
                    {
                        var message1 = new Message.BackConfimation();
                        Messenger.Send(message1);
                        if (message1.Result == MessageBoxResult.Yes)
                        {
                            Messenger.Send(new Message.BackConfimationOK());
                            IsViewModeEnable = true;
                            IsEditable = true;
                            if (lstPromotion.IndexOf(CurrentView) == -1) return;
                            lstPromotion[CurrentViewIndex - 1] = new Data.Promotion(CurrentView.ID);
                            CurrentView = lstPromotion[CurrentViewIndex - 1];
                            
                        }
                    }
                    else
                    {
                        IsViewModeEnable = true;
                        IsEditable = true;
                        Messenger.Send(new Message.BackConfimationOK());
                    }
                }
                );
                AddPhotoCommand = new RelayCommand<object>(
                    (s) => true,
                    (s) =>
                    {
                        CurrentView.AddPhoto();
                    }
                );
                DeleteCommand = new RelayCommand<Data.Promotion>(
                (s) => !IsViewModeEnable,
                (s) =>
                {
                    try
                    {
                        var message = new Message.DeleteConfirmation();
                        Messenger.Send(message);
                        if (message.Result == MessageBoxResult.No) return;

                        CurrentView.Delete();
                        lstPromotion.Remove(CurrentView);
                        IsViewModeEnable = true;
                        IsEditable = true;
                        Messenger.Send(new Message.BackConfimationOK());
                    }
                    catch
                    {
                        Messenger.Send(new Message.DeleteFailed());
                    }
                }
                );
                PreviousCommand = new RelayCommand<Data.Promotion>(
                (s) => CurrentViewIndex > 1,
                (s) =>
                {
                    --CurrentViewIndex;
                }
            );
                NextCommand = new RelayCommand<Data.Promotion>(
                    (s) => CurrentViewIndex < lstPromotion.Count,
                    (s) =>
                    {
                        ++CurrentViewIndex;
                    }
                );
                GoToPageCommand = new RelayCommand<Data.Promotion>(
                    (s) => true,
                    (s) =>
                    {
                        CurrentViewIndex = CurrentViewIndex;
                    }
                );
                SearchFilter = new ObservableCollection<string>();
                SearchFilterIndex = 0;
                SearchCommand = new RelayCommand<String>(
                    (s) => true,
                    (s) =>
                    {
                        if (!IsSearching)
                        {
                            IsSearching = true;
                            return;
                        }
                        switch (SearchFilterIndex)
                        {
                            case 0: lstPromotion = Data.Promotion.SearchByName(s);
                                break;
                            case 1: lstPromotion = Data.Promotion.SearchByNo(s);
                                break;
                        }
                    }
                );
                SearchCancelCommand = new RelayCommand<object>(
                    (s) => true,
                    (s) =>
                    {
                        IsSearching = false;
                        lstPromotion = Data.Promotion.GetAllPromotion();
                    }
                );
            }
        }
    }
